/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UI;

import Excepciones.FechaIncorrectaException;
import Excepciones.FechaSuperiorException;
import Logica.Proyecto;
import Manejadores.ManejadorMetricas;
import Persistencia.Singleton_Sesion;
import java.util.Calendar;
import java.util.Iterator;
import javax.swing.JOptionPane;

/**
 *
 * @author Cynthia
 */
public class IMetricas extends javax.swing.JFrame {

    private static int HISTORICO = 1;
    private static int MENSUAL = 2;
    private static int ANUAL = 3;
    private static int CANTIDADACCIONES = 1;
    private static int PROMEDIOTIEMPO = 2;
    private int tipoMetrica;
    private int seleccion;
    private String proyecto;
    private ManejadorMetricas manejaMetricas = new ManejadorMetricas();
    private Singleton_Sesion session;

    /**
     * Creates new form IMetricas
     */
    public IMetricas() {
        initComponents();
        
         if (session.getInstancia().getUsuarioLogueado() != null) {
            this.setTitle(this.getTitle() + " - " + session.getInstancia().getUsuarioLogueado().getNombreUsuario());
        }
          if (session.getInstancia().getUsuarioLogueado() != null)
        this.cargarProyectos(session.getInstancia().getUsuarioLogueado().getNombreUsuario());
     
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup2 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTxtAñoMensual = new javax.swing.JFormattedTextField();
        jTxtAñoAnual = new javax.swing.JFormattedTextField();
        jcmbProyectos = new javax.swing.JComboBox();
        jlblProyecto = new javax.swing.JLabel();
        jCmbMes = new javax.swing.JComboBox();
        jBCancelar = new javax.swing.JButton();
        jBCantidadAcciones = new javax.swing.JButton();
        jBPromedioTiempoAbiertas = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Métricas");
        setMaximizedBounds(new java.awt.Rectangle(0, 0, 481, 284));
        setMaximumSize(new java.awt.Dimension(481, 284));
        setMinimumSize(new java.awt.Dimension(481, 284));
        setPreferredSize(new java.awt.Dimension(481, 284));
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Seleccione la restricción de tiempo para la métrica");

        buttonGroup2.add(jRadioButton1);
        jRadioButton1.setSelected(true);
        jRadioButton1.setText("Histórico");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        buttonGroup2.add(jRadioButton2);
        jRadioButton2.setText("Mensual");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        buttonGroup2.add(jRadioButton3);
        jRadioButton3.setText("Anual");
        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });

        jLabel2.setText("Mes:");

        jLabel3.setText("Año:");

        jLabel4.setText("Año:");

        jTxtAñoMensual.setEnabled(false);

        jTxtAñoAnual.setEnabled(false);

        jcmbProyectos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jlblProyecto.setText("Proyecto");

        jCmbMes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" }));
        jCmbMes.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton1)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButton2)
                            .addComponent(jRadioButton3))
                        .addGap(32, 32, 32)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(16, 16, 16)
                                .addComponent(jTxtAñoAnual, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jCmbMes, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel3)
                        .addGap(2, 2, 2)
                        .addComponent(jTxtAñoMensual, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jlblProyecto)
                        .addGap(3, 3, 3)
                        .addComponent(jcmbProyectos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(121, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton2)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jTxtAñoMensual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCmbMes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton3)
                    .addComponent(jLabel4)
                    .addComponent(jTxtAñoAnual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcmbProyectos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblProyecto))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        jRadioButton1.getAccessibleContext().setAccessibleName("jRBHistorico");
        jRadioButton2.getAccessibleContext().setAccessibleName("jRBMensual");
        jRadioButton3.getAccessibleContext().setAccessibleName("jRBAnual");

        jBCancelar.setText("Cancelar");
        jBCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCancelarActionPerformed(evt);
            }
        });

        jBCantidadAcciones.setText("Cantidad Acciones Abiertas ");
        jBCantidadAcciones.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCantidadAccionesActionPerformed(evt);
            }
        });

        jBPromedioTiempoAbiertas.setText("Promedio Tiempo Resolución");
        jBPromedioTiempoAbiertas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBPromedioTiempoAbiertasActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jBCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jBPromedioTiempoAbiertas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jBCantidadAcciones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE, false)
                    .addComponent(jBCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jBCantidadAcciones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBPromedioTiempoAbiertas)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-481)/2, (screenSize.height-284)/2, 481, 284);
    }// </editor-fold>//GEN-END:initComponents

    private void jBCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCancelarActionPerformed
        this.dispose();        // TODO add your handling code here:
    }//GEN-LAST:event_jBCancelarActionPerformed

    private void jBPromedioTiempoAbiertasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBPromedioTiempoAbiertasActionPerformed
       
        try{this.tipoMetrica = PROMEDIOTIEMPO;
        proyecto=this.jcmbProyectos.getSelectedItem().toString();
        this.llamarManejador(tipoMetrica);
        this.jTxtAñoAnual.setText("");
        this.jTxtAñoMensual.setText("");
        this.jCmbMes.setSelectedItem("Enero");
        }
        catch(FechaIncorrectaException e){
          JOptionPane.showMessageDialog(null, e.toString(), "Error", JOptionPane.ERROR_MESSAGE);
          //   this.jTxtMes.setText(""+this.getMesActual());
        }
        catch(FechaSuperiorException e){
          JOptionPane.showMessageDialog(null, e.toString(), "Error", JOptionPane.ERROR_MESSAGE);
          this.jTxtAñoMensual.setText(""+this.getAñoActual());
          this.jTxtAñoAnual.setText(""+this.getAñoActual());
        }
        catch(NumberFormatException e1){
              
         JOptionPane.showMessageDialog(null,"Solo numeros en los campos", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jBPromedioTiempoAbiertasActionPerformed

    private void jBCantidadAccionesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCantidadAccionesActionPerformed
       try{
       // System.out.println(this.jTxtMes.getText());
        this.tipoMetrica = CANTIDADACCIONES;
        proyecto=this.jcmbProyectos.getSelectedItem().toString();
        this.llamarManejador(tipoMetrica);
        this.jTxtAñoAnual.setText("");
        this.jTxtAñoMensual.setText("");
        this.jCmbMes.setSelectedItem("Enero");
       }
        catch(FechaIncorrectaException e){
          JOptionPane.showMessageDialog(null, e.toString(), "Error", JOptionPane.ERROR_MESSAGE);
       //   this.jTxtMes.setText(""+this.getMesActual());
  
        }
       catch(FechaSuperiorException e){
          JOptionPane.showMessageDialog(null, e.toString(), "Error", JOptionPane.ERROR_MESSAGE);
          this.jTxtAñoMensual.setText(""+this.getAñoActual());
          this.jTxtAñoAnual.setText(""+this.getAñoActual());
        }
        catch(NumberFormatException e1){
              
         JOptionPane.showMessageDialog(null,"Solo numeros en los campos", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jBCantidadAccionesActionPerformed

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        this.EnablearTodo();
        this.jTxtAñoMensual.setEnabled(true);
        this.jCmbMes.setEnabled(true);
        this.seleccion = MENSUAL;
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        this.EnablearTodo();
        this.seleccion = HISTORICO;

    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton3ActionPerformed
        this.EnablearTodo();
        this.jTxtAñoAnual.setEnabled(true);
        this.seleccion = ANUAL;
    }//GEN-LAST:event_jRadioButton3ActionPerformed

    private void EnablearTodo() {//buenisimo el anme jaja
        this.jTxtAñoAnual.setEnabled(false);
        this.jCmbMes.setEnabled(false);
        this.jTxtAñoMensual.setEnabled(false);
    }

    //método encargado de verificar la opción de restricción solicitada y solicitar al manejador el reporte. Recibe el tipo de métrica según el botón
    private void llamarManejador(int tipoMetrica)  throws FechaIncorrectaException, FechaSuperiorException{
        switch (this.seleccion) {
            case (1): //si es histórico
                //deberia pasar los valores necesarios
                manejaMetricas.mostrarMetricaHistorico(proyecto, tipoMetrica);
                break;
            case (2): //si es mensual
                int mes = this.jCmbMes.getSelectedIndex()+1;
                int año = Integer.parseInt(this.jTxtAñoMensual.getText().trim());
                manejaMetricas.mostrarMetricaMensual(proyecto, mes, año, tipoMetrica);
                break;
            case (3)://si es anual
                int añoAnual = Integer.parseInt(this.jTxtAñoAnual.getText().trim());
                manejaMetricas.mostrarMetricaAnual(proyecto, añoAnual, tipoMetrica);
                break;

            default:
                manejaMetricas.mostrarMetricaHistorico(proyecto, tipoMetrica);
                break;
        }
    }
    
        
     private int getAñoActual(){
     Calendar año = Calendar.getInstance();
     return año.get(Calendar.YEAR);
     }
     
     
       public void cargarProyectos(String usuario) {

        this.jcmbProyectos.removeAllItems();
        Iterator iter = manejaMetricas.CargarProyectos(usuario);
        while (iter.hasNext()) {
            Proyecto proy = (Proyecto) iter.next();
            this.jcmbProyectos.addItem(proy.getNombre());

        }
    }
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JButton jBCancelar;
    private javax.swing.JButton jBCantidadAcciones;
    private javax.swing.JButton jBPromedioTiempoAbiertas;
    private javax.swing.JComboBox jCmbMes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JFormattedTextField jTxtAñoAnual;
    private javax.swing.JFormattedTextField jTxtAñoMensual;
    private javax.swing.JComboBox jcmbProyectos;
    private javax.swing.JLabel jlblProyecto;
    // End of variables declaration//GEN-END:variables
}
